# Makefile for 104 Protocol Library
# 电网104协议数据组装与解析库

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
AR = ar
ARFLAGS = rcs

# 构建版本配置
BUILD_VERSION ?= debug
BUILD_FLAGS = 

# 根据构建版本设置编译选项
ifeq ($(BUILD_VERSION), debug)
    BUILD_FLAGS = -DBUILD_DEBUG -O0 -g3 -DDEBUG
    CFLAGS = -Wall -Wextra -std=c99 -O0 -g3 -DDEBUG
else ifeq ($(BUILD_VERSION), release)
    BUILD_FLAGS = -DBUILD_RELEASE -O3 -DNDEBUG
    CFLAGS = -Wall -Wextra -std=c99 -O3 -DNDEBUG
else ifeq ($(BUILD_VERSION), test)
    BUILD_FLAGS = -DBUILD_TEST -O1 -g
    CFLAGS = -Wall -Wextra -std=c99 -O1 -g
else ifeq ($(BUILD_VERSION), profile)
    BUILD_FLAGS = -DBUILD_PROFILE -O2 -g -pg
    CFLAGS = -Wall -Wextra -std=c99 -O2 -g -pg
else
    BUILD_FLAGS = -DBUILD_DEBUG -O0 -g3 -DDEBUG
    CFLAGS = -Wall -Wextra -std=c99 -O0 -g3 -DDEBUG
endif

# 目录设置
SRC_DIR = .
COMMON_SRC_DIR = $(SRC_DIR)/common/src
PROTOCOL_APDU_SRC_DIR = $(SRC_DIR)/protocol/apdu/src
PROTOCOL_ASDU_SRC_DIR = $(SRC_DIR)/protocol/asdu/src
CONTROL_VALIDATOR_SRC_DIR = $(SRC_DIR)/control/validator/src
CONTROL_STATEMACHINE_SRC_DIR = $(SRC_DIR)/control/statemachine/src
CONTROL_TIMEOUT_SRC_DIR = $(SRC_DIR)/control/timeout/src
CONTROL_SEQUENCE_SRC_DIR = $(SRC_DIR)/control/sequence/src
CONTROL_CONNECTION_SRC_DIR = $(SRC_DIR)/control/connection/src
DATA_TELEMETRY_SRC_DIR = $(SRC_DIR)/data/telemetry/src
DATA_ANALOG_SRC_DIR = $(SRC_DIR)/data/analog/src
DATA_CONTROL_SRC_DIR = $(SRC_DIR)/data/control/src
DATA_ENERGY_SRC_DIR = $(SRC_DIR)/data/energy/src
DATA_PULSE_SRC_DIR = $(SRC_DIR)/data/pulse/src
DATA_SYSTEM_SRC_DIR = $(SRC_DIR)/data/system/src
TRANSFER_FILE_SRC_DIR = $(SRC_DIR)/transfer/file/src
TRANSFER_UPGRADE_SRC_DIR = $(SRC_DIR)/transfer/upgrade/src
PARAMETER_SRC_DIR = $(SRC_DIR)/parameter/src
UTILS_SRC_DIR = $(SRC_DIR)/common/utils/src
ERROR_SRC_DIR = $(SRC_DIR)/common/error/src
TYPES_SRC_DIR = $(SRC_DIR)/common/types/src
API_SRC_DIR = $(SRC_DIR)/api/src
TRANSPORT_SRC_DIR = $(SRC_DIR)/transport/src

INCLUDE_DIR = .
COMMON_INCLUDE_DIR = $(INCLUDE_DIR)/common/include
PROTOCOL_APDU_INCLUDE_DIR = $(INCLUDE_DIR)/protocol/apdu/include
PROTOCOL_ASDU_INCLUDE_DIR = $(INCLUDE_DIR)/protocol/asdu/include
CONTROL_VALIDATOR_INCLUDE_DIR = $(INCLUDE_DIR)/control/validator/include
CONTROL_STATEMACHINE_INCLUDE_DIR = $(INCLUDE_DIR)/control/statemachine/include
CONTROL_TIMEOUT_INCLUDE_DIR = $(INCLUDE_DIR)/control/timeout/include
CONTROL_SEQUENCE_INCLUDE_DIR = $(INCLUDE_DIR)/control/sequence/include
CONTROL_CONNECTION_INCLUDE_DIR = $(INCLUDE_DIR)/control/connection/include
DATA_TELEMETRY_INCLUDE_DIR = $(INCLUDE_DIR)/data/telemetry/include
DATA_ANALOG_INCLUDE_DIR = $(INCLUDE_DIR)/data/analog/include
DATA_CONTROL_INCLUDE_DIR = $(INCLUDE_DIR)/data/control/include
DATA_ENERGY_INCLUDE_DIR = $(INCLUDE_DIR)/data/energy/include
DATA_PULSE_INCLUDE_DIR = $(INCLUDE_DIR)/data/pulse/include
DATA_SYSTEM_INCLUDE_DIR = $(INCLUDE_DIR)/data/system/include
TRANSFER_FILE_INCLUDE_DIR = $(INCLUDE_DIR)/transfer/file/include
TRANSFER_UPGRADE_INCLUDE_DIR = $(INCLUDE_DIR)/transfer/upgrade/include
PARAMETER_INCLUDE_DIR = $(INCLUDE_DIR)/parameter/include
UTILS_INCLUDE_DIR = $(INCLUDE_DIR)/common/utils/include
ERROR_INCLUDE_DIR = $(INCLUDE_DIR)/common/error/include
TYPES_INCLUDE_DIR = $(INCLUDE_DIR)/common/types/include
API_INCLUDE_DIR = $(INCLUDE_DIR)/api/include
TRANSPORT_INCLUDE_DIR = $(INCLUDE_DIR)/transport/include

BUILD_DIR = ../../../build
OBJ_DIR = $(BUILD_DIR)/protocol/104/core/obj
LIB_DIR = $(BUILD_DIR)/protocol/104/core/lib

# 包含路径
INCLUDES = -I$(INCLUDE_DIR) \
           -I$(COMMON_INCLUDE_DIR) \
           -I$(PROTOCOL_APDU_INCLUDE_DIR) \
           -I$(PROTOCOL_ASDU_INCLUDE_DIR) \
               -I$(CONTROL_VALIDATOR_INCLUDE_DIR) \
               -I$(CONTROL_STATEMACHINE_INCLUDE_DIR) \
               -I$(CONTROL_TIMEOUT_INCLUDE_DIR) \
               -I$(CONTROL_SEQUENCE_INCLUDE_DIR) \
               -I$(CONTROL_CONNECTION_INCLUDE_DIR) \
           -I$(DATA_TELEMETRY_INCLUDE_DIR) \
           -I$(DATA_ANALOG_INCLUDE_DIR) \
           -I$(DATA_CONTROL_INCLUDE_DIR) \
           -I$(DATA_ENERGY_INCLUDE_DIR) \
           -I$(DATA_PULSE_INCLUDE_DIR) \
           -I$(DATA_SYSTEM_INCLUDE_DIR) \
           -I$(TRANSFER_FILE_INCLUDE_DIR) \
           -I$(TRANSFER_UPGRADE_INCLUDE_DIR) \
           -I$(PARAMETER_INCLUDE_DIR) \
           -I$(UTILS_INCLUDE_DIR) \
           -I$(ERROR_INCLUDE_DIR) \
           -I$(TYPES_INCLUDE_DIR) \
           -I$(API_INCLUDE_DIR) \
           -I$(TRANSPORT_INCLUDE_DIR) \
           -I./common/version/include \
           -I../../../common/logs/include \
           -I../../../common/utils/include \
           -I../../../common/version/include \
           -I../../../common/config/include \
           -I../../../common/network/include \
           -I../../../common/json/include \
           -I../../../third/cJSON

# 源文件
COMMON_SOURCES = $(wildcard $(COMMON_SRC_DIR)/*.c)
COMMON_VERSION_SOURCES = ../../../common/version/src/version.c

# Common模块源文件
COMMON_UTILS_SOURCES = ../../../common/utils/src/buffer_utils.c \
                      ../../../common/utils/src/memory_utils.c \
                      ../../../common/utils/src/string_utils.c
COMMON_LOGS_SOURCES = ../../../common/logs/src/logger.c
COMMON_JSON_SOURCES = ../../../common/json/src/json_utils.c \
                     ../../../common/json/src/param_parser.c
COMMON_CONFIG_SOURCES = ../../../common/config/src/cfg_utils.c
COMMON_NETWORK_SOURCES = ../../../common/network/src/network_utils.c
PROTOCOL_APDU_SOURCES = $(wildcard $(PROTOCOL_APDU_SRC_DIR)/*.c)
PROTOCOL_ASDU_SOURCES = $(wildcard $(PROTOCOL_ASDU_SRC_DIR)/*.c)
CONTROL_VALIDATOR_SOURCES = $(wildcard $(CONTROL_VALIDATOR_SRC_DIR)/*.c)
CONTROL_STATEMACHINE_SOURCES = $(wildcard $(CONTROL_STATEMACHINE_SRC_DIR)/*.c)
CONTROL_TIMEOUT_SOURCES = $(wildcard $(CONTROL_TIMEOUT_SRC_DIR)/*.c)
CONTROL_SEQUENCE_SOURCES = $(wildcard $(CONTROL_SEQUENCE_SRC_DIR)/*.c)
CONTROL_CONNECTION_SOURCES = $(wildcard $(CONTROL_CONNECTION_SRC_DIR)/*.c)
DATA_TELEMETRY_SOURCES = $(wildcard $(DATA_TELEMETRY_SRC_DIR)/*.c)
DATA_ANALOG_SOURCES = $(wildcard $(DATA_ANALOG_SRC_DIR)/*.c)
DATA_CONTROL_SOURCES = $(wildcard $(DATA_CONTROL_SRC_DIR)/*.c)
DATA_ENERGY_SOURCES = $(wildcard $(DATA_ENERGY_SRC_DIR)/*.c)
DATA_PULSE_SOURCES = $(wildcard $(DATA_PULSE_SRC_DIR)/*.c)
DATA_SYSTEM_SOURCES = $(wildcard $(DATA_SYSTEM_SRC_DIR)/*.c)
TRANSFER_FILE_SOURCES = $(wildcard $(TRANSFER_FILE_SRC_DIR)/*.c)
TRANSFER_UPGRADE_SOURCES = $(wildcard $(TRANSFER_UPGRADE_SRC_DIR)/*.c)
PARAMETER_SOURCES = $(wildcard $(PARAMETER_SRC_DIR)/*.c)
UTILS_SOURCES = $(wildcard $(UTILS_SRC_DIR)/*.c)
ERROR_SOURCES = $(wildcard $(ERROR_SRC_DIR)/*.c)
TYPES_SOURCES = $(wildcard $(TYPES_SRC_DIR)/*.c)
TRANSPORT_SOURCES = $(wildcard $(TRANSPORT_SRC_DIR)/*.c)
INTEGRATED_SOURCES = common/utils/src/protocol104_integrated.c
API_SOURCES = $(wildcard $(API_SRC_DIR)/*.c)

ALL_SOURCES = $(COMMON_SOURCES) $(COMMON_VERSION_SOURCES) $(COMMON_UTILS_SOURCES) $(COMMON_LOGS_SOURCES) $(COMMON_JSON_SOURCES) $(COMMON_CONFIG_SOURCES) $(COMMON_NETWORK_SOURCES) \
              $(PROTOCOL_APDU_SOURCES) $(PROTOCOL_ASDU_SOURCES) \
              $(CONTROL_VALIDATOR_SOURCES) $(CONTROL_STATEMACHINE_SOURCES) $(CONTROL_TIMEOUT_SOURCES) $(CONTROL_SEQUENCE_SOURCES) $(CONTROL_CONNECTION_SOURCES) \
              $(DATA_TELEMETRY_SOURCES) $(DATA_ANALOG_SOURCES) $(DATA_CONTROL_SOURCES) \
              $(DATA_ENERGY_SOURCES) $(DATA_PULSE_SOURCES) $(DATA_SYSTEM_SOURCES) \
              $(TRANSFER_FILE_SOURCES) $(TRANSFER_UPGRADE_SOURCES) $(PARAMETER_SOURCES) $(UTILS_SOURCES) \
              $(ERROR_SOURCES) $(TYPES_SOURCES) $(TRANSPORT_SOURCES) $(INTEGRATED_SOURCES) $(API_SOURCES)

# 目标文件
COMMON_OBJECTS = $(COMMON_SOURCES:$(COMMON_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
COMMON_VERSION_OBJECTS = $(COMMON_VERSION_SOURCES:../../../common/version/src/version.c=$(OBJ_DIR)/version.o)

# Common模块目标文件
COMMON_UTILS_OBJECTS = $(COMMON_UTILS_SOURCES:../../../common/utils/src/%.c=$(OBJ_DIR)/common/utils/%.o)
COMMON_LOGS_OBJECTS = $(COMMON_LOGS_SOURCES:../../../common/logs/src/%.c=$(OBJ_DIR)/common/logs/%.o)
COMMON_JSON_OBJECTS = $(COMMON_JSON_SOURCES:../../../common/json/src/%.c=$(OBJ_DIR)/common/json/%.o)
COMMON_CONFIG_OBJECTS = $(COMMON_CONFIG_SOURCES:../../../common/config/src/%.c=$(OBJ_DIR)/common/config/%.o)
COMMON_NETWORK_OBJECTS = $(COMMON_NETWORK_SOURCES:../../../common/network/src/%.c=$(OBJ_DIR)/common/network/%.o)
PROTOCOL_APDU_OBJECTS = $(PROTOCOL_APDU_SOURCES:$(PROTOCOL_APDU_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
PROTOCOL_ASDU_OBJECTS = $(PROTOCOL_ASDU_SOURCES:$(PROTOCOL_ASDU_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CONTROL_VALIDATOR_OBJECTS = $(CONTROL_VALIDATOR_SOURCES:$(CONTROL_VALIDATOR_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CONTROL_STATEMACHINE_OBJECTS = $(CONTROL_STATEMACHINE_SOURCES:$(CONTROL_STATEMACHINE_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CONTROL_TIMEOUT_OBJECTS = $(CONTROL_TIMEOUT_SOURCES:$(CONTROL_TIMEOUT_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CONTROL_SEQUENCE_OBJECTS = $(CONTROL_SEQUENCE_SOURCES:$(CONTROL_SEQUENCE_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CONTROL_CONNECTION_OBJECTS = $(CONTROL_CONNECTION_SOURCES:$(CONTROL_CONNECTION_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DATA_TELEMETRY_OBJECTS = $(DATA_TELEMETRY_SOURCES:$(DATA_TELEMETRY_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DATA_ANALOG_OBJECTS = $(DATA_ANALOG_SOURCES:$(DATA_ANALOG_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DATA_CONTROL_OBJECTS = $(DATA_CONTROL_SOURCES:$(DATA_CONTROL_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DATA_ENERGY_OBJECTS = $(DATA_ENERGY_SOURCES:$(DATA_ENERGY_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DATA_PULSE_OBJECTS = $(DATA_PULSE_SOURCES:$(DATA_PULSE_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DATA_SYSTEM_OBJECTS = $(DATA_SYSTEM_SOURCES:$(DATA_SYSTEM_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
TRANSFER_FILE_OBJECTS = $(TRANSFER_FILE_SOURCES:$(TRANSFER_FILE_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
TRANSFER_UPGRADE_OBJECTS = $(TRANSFER_UPGRADE_SOURCES:$(TRANSFER_UPGRADE_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
PARAMETER_OBJECTS = $(PARAMETER_SOURCES:$(PARAMETER_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
UTILS_OBJECTS = $(UTILS_SOURCES:$(UTILS_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
ERROR_OBJECTS = $(ERROR_SOURCES:$(ERROR_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
TYPES_OBJECTS = $(TYPES_SOURCES:$(TYPES_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
TRANSPORT_OBJECTS = $(TRANSPORT_SOURCES:$(TRANSPORT_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
INTEGRATED_OBJECTS = $(INTEGRATED_SOURCES:common/utils/src/%.c=$(OBJ_DIR)/%.o)
API_OBJECTS = $(API_SOURCES:$(API_SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

ALL_OBJECTS = $(COMMON_OBJECTS) $(COMMON_VERSION_OBJECTS) $(COMMON_UTILS_OBJECTS) $(COMMON_LOGS_OBJECTS) $(COMMON_JSON_OBJECTS) $(COMMON_CONFIG_OBJECTS) $(COMMON_NETWORK_OBJECTS) \
              $(PROTOCOL_APDU_OBJECTS) $(PROTOCOL_ASDU_OBJECTS) \
              $(CONTROL_VALIDATOR_OBJECTS) $(CONTROL_STATEMACHINE_OBJECTS) $(CONTROL_TIMEOUT_OBJECTS) $(CONTROL_SEQUENCE_OBJECTS) $(CONTROL_CONNECTION_OBJECTS) \
              $(DATA_TELEMETRY_OBJECTS) $(DATA_ANALOG_OBJECTS) $(DATA_CONTROL_OBJECTS) \
              $(DATA_ENERGY_OBJECTS) $(DATA_PULSE_OBJECTS) $(DATA_SYSTEM_OBJECTS) \
              $(TRANSFER_FILE_OBJECTS) $(TRANSFER_UPGRADE_OBJECTS) $(PARAMETER_OBJECTS) $(UTILS_OBJECTS) \
              $(ERROR_OBJECTS) $(TYPES_OBJECTS) $(TRANSPORT_OBJECTS) $(INTEGRATED_OBJECTS) $(API_OBJECTS)

# 库文件 - 使用app名+时间戳+版本标识命名
# 使用YYYYMMDDHHMM格式时间戳从编译宿主机获取
TIMESTAMP = $(shell date +"%Y%m%d%H%M")
BUILD_HOST = $(shell hostname)
BUILD_DATE = $(shell date +"%Y-%m-%d %H:%M:%S %Z")
BUILD_ISO_DATE = $(shell date -Iseconds)
APP_NAME = protocol104
VERSION_SUFFIX = $(BUILD_VERSION)
FIRMWARE_NAME = $(APP_NAME)_$(TIMESTAMP)_$(VERSION_SUFFIX)
STATIC_LIB = $(LIB_DIR)/lib$(FIRMWARE_NAME).a
SHARED_LIB = $(LIB_DIR)/lib$(FIRMWARE_NAME).so


# 示例程序
EXAMPLE = example

# 默认目标 - 构建静态库并创建latest链接
all: $(STATIC_LIB) $(LIB_DIR)/lib$(APP_NAME)_latest.a
	@echo "✅ 104协议核心库构建完成"

# 调试版本构建
debug: BUILD_VERSION=debug
debug: check_common

# 发布版本构建
release: BUILD_VERSION=release
release: check_common

# 测试版本构建
test: BUILD_VERSION=test
test: check_common

# 完整构建（静态编译）
full: check_common

# 检查common模块编译
check_common:
	@echo "Checking common modules compilation..."
	$(MAKE) -C ../../../common check_compile



# 构建transport库
transport_libs:
	@echo "Building transport libraries..."
	@echo "Transport libraries are now handled by drivers directly"

# 仅检查common模块编译（用于基础测试）
common_only: check_common directories
	@echo "✅ Common modules compilation check completed"

# 构建带时间戳的协议库并创建latest链接
$(LIB_DIR)/lib$(APP_NAME)_latest.a: $(STATIC_LIB)
	@echo "Creating timestamped protocol library..."
	@echo "App Name: $(APP_NAME)"
	@echo "Build Version: $(VERSION_SUFFIX)"
	@echo "Firmware Name: $(FIRMWARE_NAME)"
	@echo "Build Host: $(BUILD_HOST)"
	@echo "Build Date: $(BUILD_DATE)"
	@echo "ISO Date: $(BUILD_ISO_DATE)"
	@echo "Unix Timestamp: $(TIMESTAMP)"
	@echo "Library: $(STATIC_LIB)"
	@echo "✅ Generated: $(STATIC_LIB)"
	@echo "📊 Library size: $$(ls -lh $(STATIC_LIB) | awk '{print $$5}')"
	@echo "🕒 Timestamp verification: $(TIMESTAMP) = $$(echo $(TIMESTAMP) | cut -c1-4)年$$(echo $(TIMESTAMP) | cut -c5-6)月$$(echo $(TIMESTAMP) | cut -c7-8)日 $$(echo $(TIMESTAMP) | cut -c9-10)时$$(echo $(TIMESTAMP) | cut -c11-12)分"
	@echo "📁 Library contents:"
	@ar -t $(STATIC_LIB) | sed 's/^/  - /'
	@echo "🔗 Creating latest link..."
	@cp "$(STATIC_LIB)" "$(LIB_DIR)/lib$(APP_NAME)_latest.a"
	@echo "✅ Latest link: $(LIB_DIR)/lib$(APP_NAME)_latest.a"


# 固件产出目标 - 完整的固件构建和打包
firmware: $(STATIC_LIB)
	@echo "📦 Creating firmware package..."
	@echo "Firmware Name: $(FIRMWARE_NAME)"
	@echo "Firmware Timestamp: $(TIMESTAMP)"
	@echo "Firmware Version: $(VERSION_SUFFIX)"
	@echo "Firmware Host: $(BUILD_HOST)"
	@echo "Firmware Date: $(BUILD_DATE)"
	@mkdir -p $(LIB_DIR)/$(FIRMWARE_NAME)
	@cp $(STATIC_LIB) $(LIB_DIR)/$(FIRMWARE_NAME)/
	@echo "APP_NAME=$(APP_NAME)" > $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "FIRMWARE_NAME=$(FIRMWARE_NAME)" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "BUILD_VERSION=$(VERSION_SUFFIX)" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "TIMESTAMP=$(TIMESTAMP)" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "BUILD_HOST=$(BUILD_HOST)" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "BUILD_DATE=$(BUILD_DATE)" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "BUILD_ISO=$(BUILD_ISO_DATE)" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "LIBRARY_FILE=lib$(FIRMWARE_NAME).a" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "LIBRARY_SIZE=$$(ls -lh $(STATIC_LIB) | awk '{print $$5}')" >> $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt
	@echo "✅ Firmware package created: $(LIB_DIR)/$(FIRMWARE_NAME)/"
	@echo "📋 Build info:"
	@cat $(LIB_DIR)/$(FIRMWARE_NAME)/build_info.txt | sed 's/^/  /'

# 不同构建版本的目标
debug: BUILD_VERSION=debug
debug: all

release: BUILD_VERSION=release
release: all

test: BUILD_VERSION=test
test: all

# 帮助信息
help:
	@echo "104协议库构建系统"
	@echo "=================="
	@echo "可用目标:"
	@echo "  all            - 构建104协议核心静态库（默认）"
	@echo "  timestamp_lib  - 构建带时间戳的协议库"
	@echo "  firmware       - 完整的固件产出（包含构建信息和打包）"
	@echo "  common_only    - 仅构建common模块"
	@echo "  debug          - 调试版本构建"
	@echo "  release        - 发布版本构建"
	@echo "  test           - 测试版本构建"
	@echo "  clean          - 清理构建文件"
	@echo "  help           - 显示此帮助信息"
	@echo ""
	@echo "固件命名规则: app名_时间戳_版本标识"
	@echo "时间戳格式: YYYYMMDDHHMM (年月日时分)"
	@echo "版本标识: debug/release/test"
	@echo "库文件命名: lib<app名>_<时间戳>_<版本>.a"
	@echo "符号链接: lib<app名>_latest.a"
	@echo ""
	@echo "示例:"
	@echo "  make BUILD_VERSION=debug all       # 构建debug版本库（推荐）"
	@echo "  make BUILD_VERSION=release all     # 构建release版本库"
	@echo "  make BUILD_VERSION=test all        # 构建test版本库"
	@echo "  make BUILD_VERSION=debug firmware  # 构建debug版本完整固件产出"


profile: BUILD_VERSION=profile
profile: all

# 创建目录
directories:
	@mkdir -p $(OBJ_DIR)
	@mkdir -p $(LIB_DIR)
	@mkdir -p $(BUILD_DIR)/bin
	@mkdir -p $(BUILD_DIR)

# 静态库
$(STATIC_LIB): directories $(ALL_OBJECTS)
	@echo "Creating static library: $@"
	$(AR) $(ARFLAGS) $@ $(filter %.o,$^)

# 动态库
$(SHARED_LIB): $(ALL_OBJECTS)
	@echo "Creating shared library: $@"
	$(CC) -shared -o $@ $^ -L../../../../output/transport/lib -L../../../../output/lib -ltransport -lnet_transport -llogger

# 编译目标文件
$(OBJ_DIR)/%.o: $(COMMON_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# Common模块编译规则
$(OBJ_DIR)/common/utils/%.o: ../../../common/utils/src/%.c
	@mkdir -p $(dir $@)
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/common/logs/%.o: ../../../common/logs/src/%.c
	@mkdir -p $(dir $@)
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/common/json/%.o: ../../../common/json/src/%.c
	@mkdir -p $(dir $@)
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/common/config/%.o: ../../../common/config/src/%.c
	@mkdir -p $(dir $@)
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/common/network/%.o: ../../../common/network/src/%.c
	@mkdir -p $(dir $@)
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/version.o: ../../../common/version/src/version.c
	@mkdir -p $(dir $@)
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(PROTOCOL_APDU_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(PROTOCOL_ASDU_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(CONTROL_VALIDATOR_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(CONTROL_STATEMACHINE_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(CONTROL_TIMEOUT_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(CONTROL_SEQUENCE_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(CONTROL_CONNECTION_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(DATA_TELEMETRY_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(DATA_ANALOG_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(DATA_CONTROL_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(DATA_ENERGY_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(DATA_PULSE_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(DATA_SYSTEM_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(TRANSFER_FILE_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(TRANSFER_UPGRADE_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(PARAMETER_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: %.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(UTILS_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(ERROR_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(TYPES_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(API_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(OBJ_DIR)/%.o: $(TRANSPORT_SRC_DIR)/%.c
	@echo "Compiling: $<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@



# 示例程序
$(EXAMPLE): $(EXAMPLE).c $(STATIC_LIB)
	@echo "Building $(EXAMPLE)..."
	$(CC) $(CFLAGS) $(INCLUDES) $(EXAMPLE).c -L$(LIB_DIR) -L../../../../output/lib -L../../../../output/transport/lib -lprotocol104 -lversion -ltransport -lnet_transport -lmodbus_transport -lpthread -o $(BUILD_DIR)/bin/$(EXAMPLE)


# 清理
clean:
	@echo "Cleaning build files..."
	rm -rf $(OBJ_DIR)
	rm -rf $(LIB_DIR)

# 安装
install: $(STATIC_LIB) $(SHARED_LIB)
	@echo "Installing libraries..."
	@mkdir -p /usr/local/lib
	@mkdir -p /usr/local/include/protocol104
	cp $(STATIC_LIB) /usr/local/lib/
	cp $(SHARED_LIB) /usr/local/lib/
	cp $(COMMON_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(PROTOCOL_APDU_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(PROTOCOL_ASDU_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(PROTOCOL_VALIDATOR_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(DATA_TELEMETRY_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(DATA_ANALOG_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(DATA_CONTROL_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(DATA_ENERGY_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(DATA_PULSE_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(DATA_SYSTEM_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(TRANSFER_FILE_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(TRANSFER_UPGRADE_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(PARAMETER_INCLUDE_DIR)/*.h /usr/local/include/protocol104/
	cp $(UTILS_INCLUDE_DIR)/*.h /usr/local/include/protocol104/

# 卸载
uninstall:
	@echo "Uninstalling libraries..."
	rm -f /usr/local/lib/libprotocol104.a
	rm -f /usr/local/lib/libprotocol104.so
	rm -rf /usr/local/include/protocol104

# 代码格式化
format:
	@echo "Formatting code..."
	find . -name "*.c" -o -name "*.h" | xargs clang-format -i

# 代码检查
check:
	@echo "Running static analysis..."
	cppcheck --enable=all --std=c99 $(INCLUDES) $(ALL_SOURCES)



# 依赖关系
-include $(ALL_OBJECTS:.o=.d)

# 生成依赖文件
$(OBJ_DIR)/%.d: $(COMMON_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/common/utils/%.d: ../../../common/utils/src/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/common/utils/\1.o $@ : ,g' > $@

$(OBJ_DIR)/common/logs/%.d: ../../../common/logs/src/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/common/logs/\1.o $@ : ,g' > $@

$(OBJ_DIR)/common/json/%.d: ../../../common/json/src/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/common/json/\1.o $@ : ,g' > $@

$(OBJ_DIR)/common/config/%.d: ../../../common/config/src/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/common/config/\1.o $@ : ,g' > $@

$(OBJ_DIR)/common/network/%.d: ../../../common/network/src/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/common/network/\1.o $@ : ,g' > $@

$(OBJ_DIR)/version.d: ../../../common/version/src/version.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,version\.o,$(OBJ_DIR)/version.o $@,g' > $@

$(OBJ_DIR)/%.d: $(PROTOCOL_APDU_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(PROTOCOL_ASDU_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(PROTOCOL_VALIDATOR_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(DATA_TELEMETRY_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(DATA_ANALOG_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(DATA_CONTROL_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(DATA_ENERGY_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(DATA_PULSE_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(DATA_SYSTEM_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(TRANSFER_FILE_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(TRANSFER_UPGRADE_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(PARAMETER_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

$(OBJ_DIR)/%.d: $(UTILS_SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) $(INCLUDES) -MM $< | sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' > $@

.PHONY: directories clean install uninstall format check help common_only check_common debug release test profile

# 保护所有生成的库文件，防止被当作中间文件删除
.SECONDARY:
.PRECIOUS: %.a
.PRECIOUS: build/protocol/104/core/lib/%.a
